﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bussiness;

namespace WebApplication.Admin
{
    public partial class BaoCaoDoanhThuTheoNgay : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                datepicker.Text = DateTime.Now.ToString("dd/MM/yyyy");
            }
        }
        public void DoanhThu()
        {
            if (string.IsNullOrEmpty(datepicker.Text))
            {
                RegisterClientScriptBlock("Thông báo", "<script> alert('Vui lòng nhập ngày')</script>");
                return;
            }
            DateTime dt = Commomcs.convertDate(datepicker.Text);
            string ngay="Ngày :" +" "+dt.ToString("dd/MM/yyyy");
            IUnitOfWork iunitfwork = new EFUnitOfWork();
            CHITIETHOADONNHAPRepository repositoryNhap = new CHITIETHOADONNHAPRepository(new EFRepository<CHITIETHOADONNHAP>(), iunitfwork);
            PHIEUTHURepository repositoryThu = new PHIEUTHURepository(new EFRepository<PHIEUTHU>(), iunitfwork);
            PHIEUCHIRepository repositoryChi = new PHIEUCHIRepository(new EFRepository<PHIEUCHI>(), iunitfwork);
            CHITIETHOADONXUATRepository repositoryXuat = new CHITIETHOADONXUATRepository(new EFRepository<CHITIETHOADONXUAT>(), iunitfwork);
            IQueryable<PHIEUCHI> LstPhieuchi = repositoryChi.All().Where(p => p.NgayChi == dt);
            IQueryable<PHIEUTHU> LstPhieuthu = repositoryThu.All().Where(p => p.NgayThu == dt);
            IQueryable<CHITIETHOADONXUAT> LstXuat = repositoryXuat.All().Where(p => p.TINHTRANG == true && p.HOADONXUAT.NGAYXUAT == dt);
            IQueryable<CHITIETHOADONNHAP> LstChitietHDN = repositoryNhap.All().Where(p => p.HOADONNHAP.NGAYNHAP == dt && p.TINHTRANG==true );
            int tongTNTheoNgay = 0;
            int Tongchitheongay = 0;
            int TongThutheongay = 0;
            int tongxuat = 0;
            int tienmat = 0;
            if (LstXuat != null)
            {
                foreach (CHITIETHOADONXUAT HDX in LstXuat)
                {
                    tongxuat += (int)HDX.HOADONXUAT.TIENPHAITRA;
                }
            }
            if (LstChitietHDN != null)
            {
                foreach (CHITIETHOADONNHAP HDN in LstChitietHDN)
                {
                    tongTNTheoNgay += (int)HDN.HOADONNHAP.TONGTIEN;
                }
            }
            if (LstPhieuchi != null)
            {
                foreach (PHIEUCHI Chi in LstPhieuchi)
                {
                    Tongchitheongay += (int)Chi.SoTienDaTra_PC;
                }
            }
            if (LstPhieuthu != null)
            {
                foreach (PHIEUTHU Thu in LstPhieuthu)
                {
                    TongThutheongay += (int)Thu.SoTienTra_PT;
                }
            }
            tienmat = (tongxuat + TongThutheongay) - (tongTNTheoNgay + Tongchitheongay);
            var source = new Microsoft.Reporting.WebForms.ReportDataSource("BaoCaoDoanhThu",
                new Baocao.BaoCaoTongXuatNhapDt().GetXuat(ngay, dt, TongThutheongay, Tongchitheongay, tongTNTheoNgay,tongxuat,tienmat));
            ReportViewer1.LocalReport.ReportPath = @"Admin\Baocao\BaoCaoDoanhThu.rdlc";
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(source);
            ReportViewer1.LocalReport.Refresh();
        }
        protected void lbtnxem_Click(object sender, EventArgs e)
        {
            DoanhThu();
        }
    }
}